package com.foreknow.controller;

import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class GetMessageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        //调用y业务层查询s所有留言信息的方法
        GuestbookService service = new GuestbookServiceImpl();
        List<Object> list = service.querryInfo();
        out.println("list中元素的个数："+list.size()+"<br>");

        out.println("<html>");
        out.println("<head> <title>get message</title></head>");
        out.println("<body>");
        out.println("<table>");
        out.println("<tr>");
        out.println("<td>id</td>");
        out.println("<td>name</td>");
        out.println("<td>phone</td>");
        out.println("<td>email</td>");
        out.println("<td>title</td>");
        out.println("<td>content</td>");
        out.println("</tr>");

        for (int i =0;i<list.size();i++){
            Guestbook guestbook = (Guestbook) list.get(i);
            out.println("<tr>");
            out.println("<td>"+guestbook.getId()+"</td>");
            out.println("<td>"+guestbook.getName()+"</td>");
            out.println("<td>"+guestbook.getPhone()+"</td>");
            out.println("<td>"+guestbook.getEmail()+"</td>");
            out.println("<td>"+guestbook.getTitle()+"</td>");
            out.println("<td>"+guestbook.getContent()+"</td>");
            out.println("<td>"+guestbook.getCreatedtime()+"</td>");
            out.println("</tr>");
        }
        out.println("</table>");
        out.println("</body>");
        out.println("</html>");



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
